const CONFIG = require('../../../config.js');
const util = require('../../../utils/util.js')
const AREAARR = require('../../../utils/areaList.js')
let currentDay = util.formatDate(new Date());
Page({
	data: {
		navBgColor: '-webkit-gradient(linear, 0% 0%, 0% 100%,from(#1b64e5), to(#39a9ed))',
		loginInfo:null,
		areaArr: AREAARR,
		isList: [ {name: '是', id: '1'}, {name: '否', id: '0'} ],
		J002List: [ {name: '非运营', id: '1'}, {name: '运营', id: '0'} ],
		J009List: [ {name: '已解押', id: '1'}, {name: '未解押', id: '0'} ],
		J003List: [
			{ name: '轿车', id: '0' },
			{ name: '运输车', id: '1'},
			{ name: '工程车', id: '2'},
		],
		J008List: [
			{ name: '全款', id: '0' },
			{ name: '按揭银行', id: '1'},
			{ name: '按揭金融公司', id: '2'},
		],
		formData:{
			J001: '',
			J002: '',
			J003: '',
			J004: '',
			J005: '',
			J006: '',
			J007: '',
			J008: '',
			J009: '',
			J010: '',
			J011: '',
			J012: '',
			J013: '',
			J014: '',
			J015: '',
		},
		currentDate: currentDay,
		J001Value: [0,0],
		cityList: [],
		areaList: [],
		visible:false,
		currentField: '',
		productTypeid: '',
		proId: '',
	},
	// 城市选择 确定
	citySure(e) {
		let currentField = this.data.currentField;
		this.setData({
			visible: !this.data.visible,
			[`formData.${currentField}.provinceCode`]: this.data.cityCheck[0].code,
			[`formData.${currentField}.cityCode`]: this.data.cityCheck[1].code,
			// [`formData.${currentField}.areaCode`]: this.data.cityCheck[2].code,
			[`formData.${currentField}Text`]:this.data.cityCheck[0].name + '-' + this.data.cityCheck[1].name
		})
	},

	// 显示/隐藏城市选择
	pickAddress(e) {
		let {field} = e.currentTarget.dataset;
		this.setData({
			  visible: !this.data.visible,
			  currentField: field
		})
	},

	// 城市选择
	cityChange: function (e, arr){
		let thisArr;
		if(arr === undefined){
			thisArr = e.detail.value;
		}else{
			thisArr = arr;
		}
		let provinceCode = this.data.areaArr.provinceList[thisArr[0]].code;
		let cityList = this.data.areaArr.cityList[provinceCode];
		// let cityCode = cityList[thisArr[1]].code;
		// let areaList = this.data.areaArr.areaList;
		this.setData({
			cityList: cityList,
			// areaList: areaList[cityCode]
		})
		this.data.cityCheck = [this.data.areaArr.provinceList[thisArr[0]],cityList[thisArr[1]]];
		// this.data.cityCheck = [this.data.areaArr.provinceList[thisArr[0]],cityList[thisArr[1]],areaList[cityCode][thisArr[2]]];
	},

	// 
	setCity(params){
		let {provinceCode, cityCode, areaCode, itemCode} = params;
		let citys = this.data.areaArr;
		let cityArr = citys.cityList[provinceCode];
		// let areaArr = citys.areaList[cityCode];
		let area1 = 0, area2 = 0,area3 = 0;
		for(let i=0;i < citys.provinceList.length; i++){
			if(citys.provinceList[i].code === provinceCode){
				area1 = i;
			}
		}
		for(let i=0;i<cityArr.length;i++){
			if(cityArr[i].code === cityCode){
				area2 = i;
			}
		}
		
		this.cityChange(null,[area1,area2]);
		// for(let i=0;i<areaArr.length;i++){
		// 	if(areaArr[i].code === areaCode){
		// 		area3 = i;
		// 	}
		// }
		this.setData({
			[`${itemCode}cityList`]:cityArr,
			// [`${itemCode}areaList`]:areaArr,
			[`${itemCode}Value`]: [area1,area2],
			// [`${itemCode}Value`]: [area1,area2,area3],
			[`formData.${itemCode}Text`]: citys.provinceList[area1].name + '-' + cityArr[area2].name,
			// [`formData.${itemCode}Text`]: citys.provinceList[area1].name + '/' + cityArr[area2].name + '-' + areaArr[area3].name,
		})
	},
	
	// 时间选择
	bindDateChange: function (e) {
		const {field} = e.currentTarget.dataset;
		this.setData({
			applyTime: e.detail.value,
			[`formData.${field}`]: e.detail.value
		})
	},
  	// 单选
	radioChange: function (e) {
		const {field} = e.currentTarget.dataset;
		this.setData({
			[`formData.${field}`]: e.currentTarget.dataset.value
		})
	},
	// 多选
	checkChange: function (e) {
		const {field} = e.currentTarget.dataset;
		let val = e.currentTarget.dataset.value;
		// 以上皆无 
		if((field === 'creditHaveArr' && val == '4') || (field === 'creditCardHaveArr' && val == '5')){
			this.setData({
				[`formData.${field}`]: []
			})
		}else{
			let idx = this.data.formData[field].indexOf(val);
			if(idx > -1){
				this.data.formData[field].splice(idx, 1);
			}else{
				this.data.formData[field].push(val);
			}
			this.setData({
				[`formData.${field}`]: this.data.formData[field]
			})
		}
	},
	// 输入框
	formInputChange(e) {
		const {field} = e.currentTarget.dataset
		this.setData({
			[`formData.${field}`]: e.detail.value
		})
	},

	// 数据回显
	getDataFn: function(){
		let that = this;
		wx.request({
			url: CONFIG.host + "/system/appInfoController/getCustomerCarDetailInfo",
			data: {
				carId: this.data.proId,
			},
			method: "GET",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				if(res.data.code === 'success' && res.data.data){
					let data = res.data.data;
					for(let i = 0; i < data.length; i++){
						if(data[i].itemCode === 'J001'){
							that.setCity(data[i]);
							that.data.formData[data[i].itemCode] = {
								provinceCode: data[i].provinceCode,
								cityCode: data[i].cityCode,
								// areaCode: data[i].areaCode
							}
						}else{
							that.data.formData[data[i].itemCode] = data[i].itemValue;
						}
					}
					this.setData({
						formData: that.data.formData
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

	// 提交保存
	submitFn: function(){
		let datas = JSON.parse(JSON.stringify(this.data.formData));
		// 提交验证
		let isBool = datas.J001 === '' || datas.J002 === '' || datas.J003 === '' || datas.J004 === '' 
				|| datas.J005 === '' || datas.J006 === '' || datas.J008 === '' || datas.J011 === '' 
				|| datas.J013 === '';
		if(isBool){
			wx.showToast({
				title: '请填写完整！',
				icon: 'none',
			})
			return;
		}
		let productInfos = [];
		for(let key in datas){
			let obj = null;
			if(key === "J001"){
				obj = {
					"itemCode": key,
					"itemValue": '',
					"provinceCode": datas[key].provinceCode,
					"cityCode": datas[key].cityCode,
					"areaCode": datas[key].areaCode
				}
			}else if(key === 'J001Text'){
				continue;
			}else{
				if(Array.isArray(datas[key])){
					datas[key] = datas[key].join(',');
				}
				obj = {
					"itemCode": key,
					"itemValue": datas[key],
					"provinceCode": "",
					"cityCode": "",
					"areaCode": ""
				}
			}
			productInfos.push(obj);
		}
		wx.request({
			url: CONFIG.host + "/system/appInfoController/addCustomerCarInfo",
			data: {
				"carId": this.data.proId,
				"carInfos": productInfos
			},
			method: "POST",
			header: {
				'content-type': 'application/json',
				'Authorization': this.data.loginInfo.token
			},
			success: res => {
				wx.showToast({
					title: res.data.message,
					icon: 'none',
				})
				if(res.data.code === 'success'){
					wx.redirectTo({
						url: '../../productList/productList'
					})
				}
			},fail: function (error) {
				console.log(error);
			}
		})
	},

  	onLoad: function (options) {
		this.setData({
			proId: options.proId,
			productTypeid: options.productTypeid
		})
		let loginInfo = wx.getStorageSync('loginInfo') || '';
		if(loginInfo){
			this.setData({
				loginInfo : JSON.parse(loginInfo)
			})
		}
		if(options.proId){
			this.getDataFn();
		}else{
			this.cityChange(null,[0,0]);
		}
	  },
	  
	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {

	},
})
